package com.yic.module.qms.controller.admin.inspectprocess;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.item.ItemApi;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import com.yic.module.mes.api.mo.MoApi;
import com.yic.module.mes.api.mo.dto.MoRespDTO;
import com.yic.module.mes.api.process.ProcessApi;
import com.yic.module.mes.api.process.dto.ProcessRespDTO;
import com.yic.module.qms.controller.admin.inspectinfo.vo.InspectInfoRespVO;
import com.yic.module.qms.dal.dataobject.inspectscheme.InspectSchemeDO;
import com.yic.module.qms.service.inspectinfo.InspectInfoService;
import com.yic.module.qms.service.inspectscheme.InspectSchemeService;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.qms.controller.admin.inspectprocess.vo.*;
import com.yic.module.qms.dal.dataobject.inspectprocess.InspectProcessDO;
import com.yic.module.qms.convert.inspectprocess.InspectProcessConvert;
import com.yic.module.qms.service.inspectprocess.InspectProcessService;

@Tag(name = "管理后台 - 过程检验")
@RestController
@RequestMapping("/qms/inspect-process")
@Validated
public class InspectProcessController {

    @Resource
    private InspectProcessService inspectProcessService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private ItemApi itemApi;
    @Resource
    private InspectSchemeService inspectSchemeService;
    @Resource
    private ProcessApi processApi;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private MoApi moApi;
    @Resource
    private InspectInfoService inspectInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建过程检验")
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:create')")
    public CommonResult<Long> createInspectProcess(@Valid @RequestBody InspectProcessCreateReqVO createReqVO) {
        return success(inspectProcessService.createInspectProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新过程检验")
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:update')")
    public CommonResult<Boolean> updateInspectProcess(@Valid @RequestBody InspectProcessUpdateReqVO updateReqVO) {
        inspectProcessService.updateInspectProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除过程检验")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:delete')")
    public CommonResult<Boolean> deleteInspectProcess(@RequestParam("id") Long id) {
        inspectProcessService.deleteInspectProcess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得过程检验")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:query')")
    public CommonResult<InspectProcessRespVO> getInspectProcess(@RequestParam("id") Long id) {
        InspectProcessDO inspectProcess = inspectProcessService.getInspectProcess(id);
        InspectProcessRespVO resp = InspectProcessConvert.INSTANCE.convert(inspectProcess);
        //封装工厂
        FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
        if (ObjectUtil.isNotEmpty(factory)) {
            resp.setFactoryName(factory.getName());
        }
        //封装检验方案
        InspectSchemeDO inspectScheme = inspectSchemeService.getInspectScheme(resp.getSchemeId());
        if (ObjectUtil.isNotEmpty(inspectScheme)) {
            resp.setSchemeName(inspectScheme.getName());
            resp.setSchemeCode(inspectScheme.getCode());
        }
        //封装生产工单
        MoRespDTO mo = moApi.getMo(resp.getMoId());
        if (ObjectUtil.isNotEmpty(mo)) {
            resp.setMoCode(mo.getCode());
        }
        //封装产品信息
        ItemRespDTO item = itemApi.getItem(resp.getItemId());
        if (ObjectUtil.isNotEmpty(item)) {
            resp.setItemName(item.getName());
            resp.setItemCode(item.getCode());
            resp.setItemSpec(item.getSpec());
            resp.setDrawCode(item.getDrawCode());
        }
        //封装工艺
        ProcessRespDTO process = processApi.getProcess(resp.getProcessId());
        if (ObjectUtil.isNotEmpty(process)) {
            resp.setProcessName(process.getName());
            resp.setProcessCode(process.getCode());
        }
        //封装检验项明细
        List<InspectInfoRespVO> infoRespVOS = inspectInfoService.getInspectInfoListByInspectId(resp.getId());
        resp.setEntryList(infoRespVOS);
        return success(resp);
    }

    @GetMapping("/list")
    @Operation(summary = "获得过程检验列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:query')")
    public CommonResult<List<InspectProcessRespVO>> getInspectProcessList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectProcessDO> list = inspectProcessService.getInspectProcessList(ids);
        return success(InspectProcessConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得过程检验分页")
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:query')")
    public CommonResult<PageResult<InspectProcessRespVO>> getInspectProcessPage(@Valid InspectProcessPageReqVO pageVO) {
        PageResult<InspectProcessDO> pageResult = inspectProcessService.getInspectProcessPage(pageVO);
        PageResult<InspectProcessRespVO> respVo = InspectProcessConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp -> {
            //封装工厂
            FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }
            //封装检验方案
            InspectSchemeDO inspectScheme = inspectSchemeService.getInspectScheme(resp.getSchemeId());
            if (ObjectUtil.isNotEmpty(inspectScheme)) {
                resp.setSchemeName(inspectScheme.getName());
                resp.setSchemeCode(inspectScheme.getCode());
            }
            //封装生产工单
            MoRespDTO mo = moApi.getMo(resp.getMoId());
            if (ObjectUtil.isNotEmpty(mo)) {
                resp.setMoCode(mo.getCode());
            }
            //封装产品信息
            ItemRespDTO item = itemApi.getItem(resp.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                resp.setItemName(item.getName());
                resp.setItemCode(item.getCode());
                resp.setItemSpec(item.getSpec());
                resp.setDrawCode(item.getDrawCode());
            }
            //封装工艺
            ProcessRespDTO process = processApi.getProcess(resp.getProcessId());
            if (ObjectUtil.isNotEmpty(process)) {
                resp.setProcessName(process.getName());
                resp.setProcessCode(process.getCode());
            }
            //封装检测人
            AdminUserRespDTO user = adminUserApi.getUser(resp.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                resp.setUserName(user.getNickname());
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出过程检验 Excel")
    @PreAuthorize("@ss.hasPermission('qms:inspect-process:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectProcessExcel(@Valid InspectProcessExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectProcessDO> list = inspectProcessService.getInspectProcessList(exportReqVO);
        // 导出 Excel
        List<InspectProcessExcelVO> datas = InspectProcessConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "过程检验.xls", "数据", InspectProcessExcelVO.class, datas);
    }

}
